<template>
  <div>
    <a-form :form="form">
      <a-row>
        <a-col :span="8">
          <a-form-item label="账单币种" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-dict-select-tag type="list" v-decorator="['currency']"
                               :trigger-change="true" dictCode="currency" placeholder="请选择账单币种" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="发票抬头" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input
              v-decorator="['invoiceTitle', { rules: [{ required: true, message: '请输入发票抬头!' }] }]"
              placeholder="请输入发票抬头"
            >
            </a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="账单属性" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-select
              placeholder="请选择账单属性"
              style="width: 200px"
              v-decorator="['billAttr']"
            >
              <a-select-option v-for="(item, key) in selectOptions" :key="key" :value="item.value">
                {{ item.text }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="对账金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input
              v-decorator="['amount', { rules: [{ required: true, message: '账单金额不能为空!' }] }]"
            >
            </a-input>
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <span style="line-height: 38px">合计金额 : {{ totalPrice }} (仅供参考)</span>
        </a-col>
      </a-row>
      <a-row>
        <a-col span="24">
          <a-form-item label="账单注释">
            <a-textarea
              v-decorator="['notes']"
              placeholder="请输入账单注释"
              :auto-size="{ minRows: 3, maxRows: 5 }"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </div>
</template>

<script>

import { getAction, httpAction } from '@api/manage'
import { UI_CACHE_DB_DICT_DATA } from '@/store/mutation-types'

export default {
  name: 'BillForm',
  props: {
    // 数据源
    dataSource: {
      type: Array,
      required: true,
      default: () => []
    },
    totalPrice: {
      type: String,
      default: 0
    },
    formId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      formData: [],
      form: this.$form.createForm(this, { name: 'horizontal_login' }),
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      selectOptions: [],
      billType: ''
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      console.log(this.dataSource)
      this.billType = this.dataSource[0].billType
      if (this.billType == '应收') {
        this.selectOptions = [{ 'value': 1, 'text': '应收' }, { 'value': 2, 'text': '预收' }]
        this.form.setFieldsValue({ 'amount': this.totalPrice, 'currency': 'CNY', 'billAttr': 1 })
      } else {
        this.selectOptions = [{ 'value': 3, 'text': '应付' }, { 'value': 4, 'text': '预付' }]
        this.form.setFieldsValue({ 'amount': this.totalPrice, 'currency': 'CNY', 'billAttr': 3 })
      }
    },
    submitBillInfo() {
      this.form.validateFields((err, values) => {
        if (!err) {
          if (this.formId == '' || this.formId == null) {
            this.$emit('cb', false, [], '请先保存，然后再编辑中创建账单')
          } else {
            let ids = ''
            let list_id = []
            this.formData = this.dataSource
            for (let i = 0; i < this.formData.length; i++) {
              this.formData[i].buisId = this.formId
              if (this.billType == '应收') {
                this.formData[i].type = 'ys'
              } else {
                this.formData[i].type = 'yf'
              }
              ids += this.formData[i].id + ','
              list_id.push(this.formData[i].id)
            }
            ids = ids.substring(0, ids.length - 1)
            console.log(ids)
            values['costIds'] = ids
            values['billType'] = this.billType
            values['costInfoList'] = this.formData
            console.log(values)
            let url = '/box.bill/save', method = 'post'
            httpAction(url, values, method).then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('cb', true, list_id, res.result)
              } else {
                this.$message.warning(res.message)
                this.$emit('cb', false, [], '')
              }
            })
          }
        } else {
          this.$emit('cb', false, [], '')
        }
      })
    }
  }
}
</script>

<style scoped>

</style>